package com.microcity.autocomponent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.microcity.autocomponent.autoview.numberpicker.NumberPickerView;

public class NumPickerActivity extends AppCompatActivity {

    private NumberPickerView mPickH;
    private NumberPickerView mPickM;
    private TextView time;
    private CheckBox canuse;
    private CheckBox canwrap;
    private LinearLayout ll;
    private View view;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            time.setText((String) msg.obj);
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_num_picker);
        initView();
    }

    private void initView() {
        time = (TextView) findViewById(R.id.text_time);
        canuse = (CheckBox)findViewById(R.id.can_use);
        canwrap = (CheckBox)findViewById(R.id.can_wrap);
        view = findViewById(R.id.view);
        ll = (LinearLayout)findViewById(R.id.time_picker);
        mPickH = (NumberPickerView) findViewById(R.id.picker_hour);
        mPickM = (NumberPickerView) findViewById(R.id.picker_minute);

        canwrap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPickH.setWrapSelectorWheel(canwrap.isChecked());
                mPickM.setWrapSelectorWheel(canwrap.isChecked());
            }
        });

        canuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (canuse.isChecked()) {
                    mPickH.setCanTouch(true);
                    mPickM.setCanTouch(true);
                    mPickH.setSelectedTextColor(ContextCompat.getColor(NumPickerActivity.this,R.color.time_can_select));
                    mPickM.setSelectedTextColor(ContextCompat.getColor(NumPickerActivity.this,R.color.time_can_select));
                } else {
                    mPickH.setCanTouch(false);
                    mPickM.setCanTouch(false);
                    mPickH.setSelectedTextColor(ContextCompat.getColor(NumPickerActivity.this,R.color.time_cannot_select));
                    mPickM.setSelectedTextColor(ContextCompat.getColor(NumPickerActivity.this,R.color.time_cannot_select));
                }
            }
        });

        mPickH.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                String h = mPickH.getContentByCurrValue();
                String m = mPickM.getContentByCurrValue();
                if (Looper.myLooper() == Looper.getMainLooper()) {
                    time.setText(h + "时" + m + "分");
                } else {
                    Message message = Message.obtain();
                    message.obj = h + "时" + m + "分";
                    mHandler.sendMessage(message);
                }
            }
        });

        mPickM.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
                String h = mPickH.getContentByCurrValue();
                String m = mPickM.getContentByCurrValue();
                if (Looper.myLooper() == Looper.getMainLooper()) {
                    time.setText(h + "时" + m + "分");
                } else {
                    Message message = Message.obtain();
                    message.obj =
                            mHandler.sendMessage(message);
                }
            }
        });
    }


}
